use assert_cmd::Command;
use assert_fs::prelude::*;
use color_eyre::eyre::Result;
use predicates::prelude::*;

#[test]
fn it_help() -> Result<()> {
  let mut cmd = Command::cargo_bin("garden")?;
  let assert = cmd.arg("--help").assert();

  assert.success().stderr("");

  Ok(())
}

#[test]
fn it_write_help() -> Result<()> {
  let mut cmd = Command::cargo_bin("garden")?;
  let assert = cmd.arg("write").arg("--help").assert();
  assert.success().stderr("");

  Ok(())
}

#[test]
fn it_writed_help() -> Result<()> {
  let mut cmd: Command = Command::cargo_bin("garden")?;
  let assert = cmd.arg("--help").arg("write").assert();
  assert.success().stderr("");
  Ok(())
}

// EDITOR="code -w" cargo run -- write -t atitle
#[test]
#[ignore]
fn it_write() {
  let temp_dir = assert_fs::TempDir::new().unwrap();

  let mut cmd = Command::cargo_bin("garden").unwrap();
  let fake_editor_path = std::env::current_dir()
    .expect("Expect to be in a dir")
    .join("tests")
    .join("fake_editor.sh");

  if !fake_editor_path.exists() {
    panic!("fake editor shell script colud not be found")
  }

  let assert = cmd
    .env("EDITOR", fake_editor_path.into_os_string())
    .env("GARDEN_PATH", temp_dir.path())
    .arg("write")
    .arg("-t")
    .arg("atitle")
    .write_stdin("N\n".as_bytes())
    .assert();

  assert.success();

  temp_dir
    .child("atitle.md")
    .assert(predicate::path::exists());
}

fn setup_command() -> (Command, assert_fs::TempDir) {
  let temp_dir = assert_fs::TempDir::new().unwrap();

  let mut cmd = Command::cargo_bin("garden").unwrap();
  let fake_editor_path = std::env::current_dir()
    .expect("Expect to be in a dir")
    .join("tests")
    .join("fake_editor.sh");

  if !fake_editor_path.exists() {
    panic!("fake editor shell script colud not be found")
  }

  cmd
    .env("EDITOR", fake_editor_path.into_os_string())
    .env("GARDEN_PATH", temp_dir.path());

  (cmd, temp_dir)
}

// EDITOR="code -w" cargo run -- write -t atitle
#[test]
#[ignore]
fn it_write_with_title() {
  let (mut cmd, temp_dir) = setup_command();

  let assert = cmd
    .arg("write")
    .arg("-t")
    .arg("atitle")
    .write_stdin("N\n".as_bytes())
    .assert();

  assert.success();

  temp_dir
    .child("atitle.md")
    .assert(predicate::path::exists());
}

// EDITOR="code -w" cargo run -- write -t atitle
#[test]
#[ignore]
fn it_write_with_written_title() {
  let (mut cmd, temp_dir) = setup_command();

  let assert = cmd.arg("write").write_stdin("N\n".as_bytes()).assert();

  assert.success();

  temp_dir
    .child("testing.md")
    .assert(predicate::path::exists());
}
